/*
* Copyright (C) 2008-2009 J-P Nurmi jpnurmi@gmail.com
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* $Id: sessiontabwidget.h 373 2009-05-05 22:40:51Z jpnurmi $
*/

#ifndef SESSIONTABWIDGET_H
#define SESSIONTABWIDGET_H

#include <QHash>
#include <QTabWidget>

class IrcModel;
class MessageView;
class MessageParser;
class SettingsWidget;
struct Connection;
namespace Irc { class Session; }

class SessionTabWidget : public QTabWidget
{
    Q_OBJECT

public:
    SessionTabWidget(Irc::Session* session, IrcModel* model, QWidget* parent = 0);
    ~SessionTabWidget();

    Irc::Session* session() const;
    uint hashKey() const;

    int zoomLevel() const;
    void setZoomLevel(int level);

public slots:
    void zoomIn();
    void zoomOut();
    void save();
    void restore();

signals:
    void alertStarted();
    void tabIconChanged(const QIcon& icon);
    void tabTextChanged(const QString& text);
    void connectTo(const QString& host, quint16 port, const QString& password, const QString& nick);
    void connectToImpl(const Connection& connection);
    void disconnectFrom(const QString& message);
    void quit(const QString& message);

protected slots:
    void on_connected();
    void on_disconnected();

    void on_msgJoined(const QString& origin, const QString& channel);
    void on_msgParted(const QString& origin, const QString& channel, const QString& message);
    void on_msgQuit(const QString& origin, const QString& message);
    void on_msgNickChanged(const QString& origin, const QString& nick);
    void on_msgModeChanged(const QString& origin, const QString& receiver, const QString& mode, const QString& args);
    void on_msgTopicChanged(const QString& origin, const QString& channel, const QString& topic);
    void on_msgInvited(const QString& origin, const QString& receiver, const QString& channel);
    void on_msgKicked(const QString& origin, const QString& channel, const QString& nick, const QString& message);
    void on_msgMessageReceived(const QString& origin, const QString& receiver, const QString& message);
    void on_msgNoticeReceived(const QString& origin, const QString& receiver, const QString& message);
    void on_msgCtcpRequestReceived(const QString& origin, const QString& request);
    void on_msgCtcpReplyReceived(const QString& origin, const QString& reply);
    void on_msgCtcpActionReceived(const QString& origin, const QString& receiver, const QString& action);
    void on_msgNumericMessageReceived(const QString& origin, uint code, const QStringList& params);
    void on_msgUnknownMessageReceived(const QString& origin, const QStringList& params);

private slots:
    void clearView(const QString& view = QString());
    void closeCurrentView(const QString& message = QString());
    void openView(const QString& receiver);
    void closeView(const QString& receiver, const QString& message = QString());
    void tabActivated(int index = -1);
    void moveToNextTab();
    void moveToPrevTab();
    void send(const QString& msg);
    void alert();
    void logMessage(const QString& receiver, const QString& message);
    void logNotice(const QString& receiver, const QString& message);
    void logCtcpAction(const QString& receiver, const QString& message);
    void logHelp(const QString& message);
    void updateTabIcon();

private:
    QString prepareTarget(const QString& sender, const QString& receiver, bool create = true, bool highlight = true);

    QHash<QString, MessageView*> views;
    QTimer* alertTimer;
    QStringList alertees;
    IrcModel* model;
    QIcon emptyIcon;
    SettingsWidget* settings;

    Irc::Session* sessio;
    MessageParser* parser;
};

#endif // SESSIONTABWIDGET_H
